package OoDay04;

public class PoiymorphicDemo {
    public static void main(String[] args) {
       /* Dog o1=new Dog("小黑",2,"黑");//狗是狗
        //o1.name/age/color/drink/eat/swim/lookhome
        System.out.println(o1.name);

        Animal o2=new Dog("小黑",2,"黑");//狗是动物。。。向上造型

        System.out.println(o2.color);
        o2.drink();
        Swim o3=new Dog("小黑",2,"黑");
        o3.swim();*/
        //演示向上造型第一点应用
        /*Master master=new Master();
        Dog dog=new Dog("小黑",2,"黑");
        master.feed(dog);     //系统自动向上造型
        Chick chick=new Chick("小花",1,"花");
        master.feed(chick);
        Fish fish=new Fish("小红",1,"红");
        master.feed(fish);

         */

        //演示向上造型的第二点应用
        //Animal o=new Animal();//编译错误，抽象类不能被实例化
        Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",1,"黑");//向上造型
        animals[1]=new Dog("小白",1,"白");
        animals[2]=new Fish("小花",1,"花");
        animals[3]=new Fish("小红",1,"红");
        animals[4]=new Chick("小灰",1,"灰");
        for (int i=0;i<animals.length;i++){  //遍历所有动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].drink();//每个动物喝水
            animals[i].eat();//每个动物吃饭
            if (animals[i] instanceof Dog){
                Dog dog=(Dog)animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){  //适用于所有实现Swim接口的（会游泳的
                Swim s=(Swim) animals[i];
                s.swim();
            }

        }

        /*Animal o=new Dog("",2,"");
        Dog g=(Dog)o;
        Swim s=(Swim)o;
        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Fish);
        if (o instanceof Fish){
            Fish f=(Fish)o;
        }*/


    }

}
